<?php

class Admin_LoginController extends Yaf_Controller_Abstract {

    private $_layout;

    public function init() {
        /* use layout */
        $this->_dispatcher = Yaf_Registry::get("dispatcher");
        $this->_layout = new LayoutPlugin('layout.phtml');
        $this->_dispatcher->registerPlugin($this->_layout);
    }

    /*
     * login index
     */

    public function IndexAction() {
        $type = $this->getRequest()->getParam("type", 0);
        $this->_view->system_type = 1;
        $this->_view->title = "用户管理系统";
        /* layout */
        $this->_layout->meta_title = '管理员登录';
        $this->_view->system_url = "/admin_Login/index/type/1";
        $this->_view->system_name = "点击切换到iTools登录后台";
        /*
        if ($type == 0) {
            $this->_view->title = "用户管理系统";
          
            $this->_layout->meta_title = '管理员登录';
            $this->_view->system_url = "/admin_Login/index/type/1";
            $this->_view->system_name = "点击切换到iTools登录后台";
        } else {
            $this->_view->title = "iTools登录后台";
  
            $this->_layout->meta_title = 'iTools用户登录';
            $this->_view->system_url = "/admin_Login/index/type/0";
            $this->_view->system_name = "点击切换到用户管理系统";
        }*/
        //设置redis访问（设置key前缀）
        /*
          $redis = new cache_redis();
          $redis->redis()->setOption(Redis::OPT_PREFIX, 'myAppName');
          $key = 'test';
          $value = '这是一个测试！';
          $redis->set($key, $value);
          $fvalue = $redis->get('fields'); */
    }

    /*
     * is_login
     */

    public function LoginsAction() {
        $username = base64_decode($this->getRequest()->getPost('username', ''));
        $password = base64_decode($this->getRequest()->getPost('password', ''));
        $captcha = base64_decode($this->getRequest()->getPost('captcha', ''));
        $system_type = base64_decode($this->getRequest()->getPost('system_type', ''));
        $_SESSION['system_type'] = 1;
        if (empty($captcha)) {
            echo '101:请填写验证码';
            exit;
        }
        if (strtolower($_SESSION['admin_captcha']) != strtolower($captcha)) {
            echo '101:验证码错误';
            exit;
        }
        $password = md5($password);
        $login_status = new AdminModel();
        $info = $login_status->login_sign($username, $password);
        if (!empty($info)) {
            $_SESSION['info'] = $info;
            unset($info);
            echo '1:登录成功！';
            exit;
        } else {
            echo '101:用户名或密码错误';
            exit;
        }
    }

    /*
     * admin logout
     * 
     */

    public function LogoutAction() {
        unset($_SESSION['info']);
        header("Location:index/type/0");
        /*
        if ($_SESSION['system_type'] == 0) {
            header("Location:index/type/0");
        } else {
            header("Location:index/type/1");
        }*/
    }

}
